﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TiposComuns.Objectos;
using System.Runtime.CompilerServices;

namespace TiposComuns.Objectos
{
    /// <summary>
    /// State of a server. Contains it's view of the global state, wich is the
    /// servers it thinks that are alive and who is the primary. It also
    /// indicates the last action executed.
    /// </summary>
    [Serializable]
    public class State
    {
        #region Constructors
        public State()
        {
            this.View = new View();
            this.IdMensagem = new IDMsg();
            this.IdPendentes = 0;
        }
        #endregion

        #region Modifiers
        /// <summary>
        /// View of the global state
        /// </summary>
        public View View
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            get;

            [MethodImpl(MethodImplOptions.Synchronized)]
            set; 
        }

        /// <summary>
        /// Identifier of the last action executed
        /// </summary>
        public IDMsg IdMensagem
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            get;

            [MethodImpl(MethodImplOptions.Synchronized)]
            set; 
        }

        public int IdPendentes
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            get;

            [MethodImpl(MethodImplOptions.Synchronized)]
            set;
        }

        #endregion

        #region Methods
        public bool IsOK()
        {
            return this.View.ViewState.Count > 1;
        }

        public State Clone()
        {
            State state = new State();
            state.IdMensagem = new IDMsg(this.IdMensagem.ID, this.IdMensagem.isClient);
            state.View = new View(this.View.VID, new List<int>());

            state.View.ViewState.AddRange(this.View.ViewState);

            return state;
        }

        #endregion
    }
}
